package com.autoboot.controller;

import com.autoboot.domain.bo.queryBo;
import com.autoboot.domain.query.QueryMultiBo;
import com.autoboot.domain.vo.R;
import com.autoboot.service.DynamicTableService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 通用表控制器
 * @author Maosw
 */
@RestController
@RequestMapping("/api/table")
public class TableController {

    @Autowired
    private DynamicTableService dynamicTableService;

    /**
     * 获取表中所有数据
     *
     * @param tableName 表名
     * @return 数据列表
     */
    @GetMapping("/{tableName}")
    public R<List<Map<String, Object>>> list(@PathVariable String tableName) {
        return R.ok(dynamicTableService.listFromTable(tableName));
    }

    /**
     * 根据ID获取表中的数据
     *
     * @param tableName 表名
     * @param idField ID字段名
     * @param id ID值
     * @return 数据详情
     */
    @GetMapping("/{tableName}/{idField}/{id}")
    public R<Map<String, Object>> getById(@PathVariable String tableName, @PathVariable String idField, @PathVariable Object id) {
        return R.ok(dynamicTableService.getByIdFromTable(tableName, idField, id));
    }

    /**
     * 条件查询表数据
     *
     * @return 数据列表
     */
    @PostMapping("/query")
    public R<List<Map<String, Object>>> search(@RequestBody queryBo queryBo) {
        // 使用增强的QueryWrapper处理复杂条件
        return R.ok(dynamicTableService.listByConditionFromTable(
                queryBo.getTableName(),
                queryBo.getParams(),
                queryBo.getLikes(),
                queryBo.getGt(),
                queryBo.getGe(),
                queryBo.getLt(),
                queryBo.getLe(),
                queryBo.getBetween(),
                queryBo.getOrders()));
    }

    /**
     * 分页查询表数据
     *
     * @return 分页数据
     */
    @PostMapping("/page")
    public R<Page<Map<String, Object>>> page(@RequestBody queryBo queryBo) {
        // 将String类型的分页参数转为long
        long current = queryBo.getPageIndex() != null ? Long.parseLong(queryBo.getPageIndex()) : 1;
        long size = queryBo.getPageSize() != null ? Long.parseLong(queryBo.getPageSize()) : 10;

        // 使用增强的QueryWrapper处理复杂条件
        return R.ok(dynamicTableService.pageFromTable(
                queryBo.getTableName(),
                current,
                size,
                queryBo.getParams(),
                queryBo.getLikes(),
                queryBo.getGt(),
                queryBo.getGe(),
                queryBo.getLt(),
                queryBo.getLe(),
                queryBo.getBetween(),
                queryBo.getOrders()));
    }

    /**
     * 分页查询多表数据
     * @param queryMultiBo
     * @return
     */
    @PostMapping("/page/multi")
    public R<Page<Map<String, Object>>> pageMulti(@RequestBody QueryMultiBo queryMultiBo) {
        return R.ok(dynamicTableService.pageMultiFromTable(queryMultiBo));
    }


    /**
     * 保存数据到表
     *
     * @param tableName 表名
     * @param data 数据Map
     * @return 保存结果
     */
    @PostMapping("/{tableName}")
    public R<Boolean> save(@PathVariable String tableName, @RequestBody Map<String, Object> data) {
        return R.ok(dynamicTableService.saveToTable(tableName, data));
    }

    /**
     * 批量保存数据到表
     *
     * @param tableName 表名
     * @param dataList 数据列表
     * @return 保存结果
     */
    @PostMapping("/{tableName}/batch")
    public R<Boolean> saveBatch(@PathVariable String tableName, @RequestBody List<Map<String, Object>> dataList) {
        return R.ok(dynamicTableService.saveBatchToTable(tableName, dataList));
    }

    /**
     * 更新表数据
     *
     * @param tableName 表名
     * @param id ID值
     * @param data 数据Map
     * @return 更新结果
     */
    @PutMapping("/{tableName}/{idField}/{id}")
    public R<Boolean> update(@PathVariable String tableName, @PathVariable String idField, @PathVariable Object id, @RequestBody Map<String, Object> data) {
        // 确保ID存在于数据中
        data.put(idField, id);
        return R.ok(dynamicTableService.updateToTable(tableName, idField, data));
    }

    /**
     * 删除表数据
     *
     * @param tableName 表名
     * @param id ID值
     * @return 删除结果
     */
    @DeleteMapping("/{tableName}/{idField}/{id}")
    public R<Boolean> delete(@PathVariable String tableName, @PathVariable String idField, @PathVariable Object id) {
        return R.ok(dynamicTableService.removeFromTable(tableName, idField, id));
    }

    /**
     * 执行自定义SQL查询
     *
     * @param sqlStatement SQL语句
     * @param paramMap 参数Map
     * @return 查询结果
     */
    @PostMapping("/execute")
    public R<List<Map<String, Object>>> executeQuery(@RequestParam String sqlStatement, @RequestBody(required = false) Map<String, Object> paramMap) {
        if (paramMap == null) {
            paramMap = new HashMap<>();
        }
        return R.ok(dynamicTableService.executeQuery(sqlStatement, paramMap));
    }

    /**
     * 根据表名获取表结构
     * @param tableName
     * @return
     */
    @GetMapping("/{tableName}/structure")
    public R<List<Map<String, Object>>> getTableStructure(@PathVariable String tableName) {
        return R.ok(dynamicTableService.getTableStructure(tableName));
    }
}
